/*
 * Author: sgxiao
 * Date : 2009年 2月18日 星期三 00时14分31秒 CST
 * Version	: 0.2
 * history
 *	sgxiao	0.2 change for SBO
 *
 */

import java.io.*;
import java.util.*;

class ReturnRow {
	private String dateOfMonth = "0";
	private double currentMonthReturn = 0.0;
	private double currentMonthReturnAccumulated = 0.0;
	private double returnRateCompareToLastMonth = 0.0;
	private double returnRateCompareToLastMonthAccumulated = 0.0;
	private double column2MinusColumn4 = 0.0;
	private double column3MinusColumn5 = 0.0;

	public double getCurrentMonthReturn() {
		return currentMonthReturn;
	}

	public void setCurrentMonthReturn(double currentMonthReturn) {
		this.currentMonthReturn = currentMonthReturn;
	}

	public double getCurrentMonthReturnAccumulated() {
		return currentMonthReturnAccumulated;
	}

	public void setCurrentMonthReturnAccumulated(
			double currentMonthReturnAccumulated) {
		this.currentMonthReturnAccumulated = currentMonthReturnAccumulated;
	}

	public double getReturnRateCompareToLastMonth() {
		return returnRateCompareToLastMonth;
	}

	public void setReturnRateCompareToLastMonth(
			double returnRateCompareToLastMonth) {
		this.returnRateCompareToLastMonth = returnRateCompareToLastMonth;
	}

	public double getReturnRateCompareToLastMonthAccumulated() {
		return returnRateCompareToLastMonthAccumulated;
	}

	public void setReturnRateCompareToLastMonthAccumulated(
			double returnRateCompareToLastMonthAccumulated) {
		this.returnRateCompareToLastMonthAccumulated = returnRateCompareToLastMonthAccumulated;
	}

	public double getColumn2MinusColumn4() {
		return column2MinusColumn4;
	}

	public void setColumn2MinusColumn4(double column2MinusColumn4) {
		this.column2MinusColumn4 = column2MinusColumn4;
	}

	public double getColumn3MinusColumn5() {
		return column3MinusColumn5;
	}

	public void setColumn3MinusColumn5(double column3MinusColumn5) {
		this.column3MinusColumn5 = column3MinusColumn5;
	}

	public String getDateOfMonth() {
		return dateOfMonth;
	}

	public void setDateOfMonth(String dateOfMonth) {
		this.dateOfMonth = dateOfMonth;
	}

	public void addCurrentMonthReturn(double currentMonthReturn) {
		this.currentMonthReturn += currentMonthReturn;
	}

	public void addCurrentMonthReturnAccumulated(
			double currentMonthReturnAccumulated) {
		this.currentMonthReturnAccumulated += currentMonthReturnAccumulated;
	}

	public void addReturnRateCompareToLastMonth(
			double returnRateCompareToLastMonth) {
		this.returnRateCompareToLastMonth += returnRateCompareToLastMonth;
	}

	public void addReturnRateCompareToLastMonthAccumulated(
			double returnRateCompareToLastMonthAccumulated) {
		this.returnRateCompareToLastMonthAccumulated += returnRateCompareToLastMonthAccumulated;
	}

	public void addColumn2MinusColumn4(double column2MinusColumn4) {
		this.column2MinusColumn4 += column2MinusColumn4;
	}

	public void addColumn3MinusColumn5(double column3MinusColumn5) {
		this.column3MinusColumn5 += column3MinusColumn5;
	}
}

/**
 * @auther sgxiao
 * @version 0.2
 * @param args
 *            [0] the tickers list file name
 * @param args
 *            [1] the ticker's whole.return file name
 */
public class AverageCaculator {
	public static void main(String[] args) throws FileNotFoundException {

		String tickerList = args[0];
		String file = args[1];
		///**/System.out.println(tickerList);
		List<ReturnRow> rows = new Vector<ReturnRow>();
		Scanner scannerTickerList = new Scanner(new File(tickerList));
		int count = 0;
		boolean isFirstTurn = true;
		while (scannerTickerList.hasNext()) {
			int i = 0;
			
			String ticker = scannerTickerList.next();
			Scanner scannerFile = new Scanner(new File(ticker
					+ "." + file + ".sbo.whole.return"));
			///**/System.err.println("ticker " + ticker);
			
			while (scannerFile.hasNext()) {
				ReturnRow row = new ReturnRow();
				///**/System.err.println("into " + i);
				String date = scannerFile.next();
				
				if ( !date.equals("0") ) {
					row.setDateOfMonth(date);
				}
				row.setCurrentMonthReturn(scannerFile.nextDouble());
				row.setCurrentMonthReturnAccumulated(scannerFile.nextDouble());
				row.setReturnRateCompareToLastMonth(scannerFile.nextDouble());
				row.setReturnRateCompareToLastMonthAccumulated(scannerFile.nextDouble());
				row.setColumn2MinusColumn4(scannerFile.nextDouble());
				row.setColumn3MinusColumn5(scannerFile.nextDouble());
				
				if (isFirstTurn) {
					
					
					///**/System.out.printf("%s\t%.15f\n", row.getDateOfMonth(), row.getCurrentMonthReturn());
					
					
					rows.add(row);
				} else {
					
					///**/System.out.printf("%s\t%.15f\n", row.getDateOfMonth(), row.getCurrentMonthReturn());
					
					ReturnRow temp = rows.get(i);
					temp.addColumn2MinusColumn4(row.getColumn2MinusColumn4());
					temp.addColumn3MinusColumn5(row.getColumn3MinusColumn5());
					temp.addCurrentMonthReturn(row.getCurrentMonthReturn());
					temp.addCurrentMonthReturnAccumulated(row
							.getCurrentMonthReturnAccumulated());
					temp.addReturnRateCompareToLastMonth(row
							.getReturnRateCompareToLastMonth());
					temp.addReturnRateCompareToLastMonthAccumulated(row
							.getReturnRateCompareToLastMonthAccumulated());
				}
				i++;
			}
			isFirstTurn = false;
			count++;
		}
		
		///**/System.err.println("count " + count);
		Iterator<ReturnRow> iter = rows.iterator();
		while (iter.hasNext()) {
			ReturnRow t = (ReturnRow) iter.next();
			System.out.printf(
					"%s\t%.15f\t%.15f\t%.15f\t%.15f\t%.15f\t%.15f\n", t
							.getDateOfMonth(), t.getCurrentMonthReturn() / count, t
							.getCurrentMonthReturnAccumulated() / count, t
							.getReturnRateCompareToLastMonth() / count, t
							.getReturnRateCompareToLastMonthAccumulated() / count, t
							.getColumn2MinusColumn4() / count, t
							.getColumn3MinusColumn5() / count);		}
		
	}
}
